#include "mainwindow.h"
#include <QLabel>
#include <QLineEdit>
#include <QResizeEvent>
#include <QTimer>
#include "../modules/notice/elalert.h"
#include "../modules/basic/elbutton.h"
#include "../modules/form/elcheckbox.h"
#include "../modules/others/eldrawer.h"
#include "../modules/basic/elicon.h"
#include "../modules/form/elinput.h"
#include "../modules/notice/elloading.h"
#include "../modules/notice/elmessage.h"
#include "../modules/notice/elnotification.h"
#include "../modules/form/elradio.h"
#include "../modules/data/elresult.h"
#include "../modules/form/elslider.h"
#include "../modules/form/elswitch.h"
#include "../modules/data/eltag.h"
#include "../modules/others/eltooltip.h"
#include "ui_mainwindow.h"

void MainWindow::resizeEvent(QResizeEvent *e)
{
    ElMessage::adjustifyAllPos();
    ElNotification::adjustifyAllPos();
    emit resized();
    e->accept();
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // custom theme color
    // ElTheme::togglePrimaryColor(QColor::fromString("#fe2857"));

    // ElButton, ElCircleButton, ElTextButton, ElLink
    auto btns = testElButtons();

    // ElCheckbox
    auto chb = testElCheckbox();

    // ElRadio
    testElRadio();

    // ElSlider
    testElSlider();

    // ElSwitch
    testElSwitch();

    // ElMessage
    testElMessage(btns);

    // ElLoading
    testElLoading();

    // ElAlert
    testElAlert();

    // ElNotification
    testElNotification(chb);

    // ElResult
    testElResult();

    // ElInput
    testElInput();

    // ElTag
    testElTag();

    // ElDrawer
    testElDrawer();
}

MainWindow::~MainWindow()
{
    delete ui;
}

QList<ElButton *> MainWindow::testElButtons()
{
    QList<ElButton *> btns;
    // ElButton
    QString texts[] = {"默认按钮", "主要按钮", "成功按钮", "警告按钮", "危险按钮", "信息按钮"};
    for (int i = 0; i < 3; ++i) {
        if (i == 1)
            texts[0] = "朴素按钮";
        else if (i == 2)
            texts[0] = "圆角按钮";
        for (int j = 0; j < 6; ++j) {
            auto *btn = new ElButton(this);
            btns << btn;
            auto tooltip = new ElTooltip(btn, tr("Button"));
            tooltip->setEffect(ElTooltip::Light);
            tooltip->setHideAfter(2000);
            // tooltip->setOpenDelay(800);
            // tooltip->setOffset(10);
            // tooltip->setManual(true);
            if (i == 1)
                tooltip->setPlacement(ElTooltip::Placement(j));
            else
                tooltip->setPlacement(ElTooltip::Placement(6 + j));
            btn->setTooltip(tooltip);
            btn->setText(texts[j]);
            btn->setType(ElButton::Type(i));
            btn->setTheme(ElTheme::Type(j));
            btn->setIcon(":/icons/warning.svg");
            QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
                tooltip->setEnabled(false);
                btn->setEnabled(false);
                QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
                    tooltip->setEnabled(true);
                    btn->setEnabled(true);
                });
            });
            btn->move(20 + 120 * j, 25 + i * 60);
        }
    }

    // ElCircleButton
    QString icons[] = {"search", "edit", "check", "star", "delete", "message"};
    for (int i = 0; i < 6; ++i) {
        auto btn = new ElCircleButton(this);
        btns << btn;
        btn->setTooltip(new ElTooltip(btn, tr("CircleButton")));
        btn->setIcon(QString(":/icons/%1.svg").arg(icons[i]));
        btn->setTheme(ElTheme::Type(i));
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
            btn->setEnabled(false);
            QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { btn->setEnabled(true); });
        });
        btn->move(50 + 120 * i, 200);
    }

    // ElTextButton
    texts[0] = "文字按钮";
    for (int i = 0; i < 6; ++i) {
        auto btn = new ElTextButton(this);
        btns << btn;
        btn->setText(texts[i]);
        btn->setIcon(QString(":/icons/%1.svg").arg(icons[i]), 16, ElAbstractButton::Prepend);
        btn->setTheme(ElTheme::Type(i));
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
            btn->setEnabled(false);
            QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { btn->setEnabled(true); });
        });
        btn->move(20 + 120 * i, 260);
    }

    // ElLink
    texts[0] = "文字链接";
    for (int i = 0; i < 6; ++i) {
        auto link = new ElLink(this);
        btns << link;
        link->setText(texts[i]);
        link->setIcon(QString(":/icons/%1.svg").arg(icons[i]));
        link->setTheme(ElTheme::Type(i));
        link->setTarget(
            QUrl::fromUserInput("https://element.eleme.cn/#/zh-CN/component/installation"));
        // link->setUnderline(false);
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
            link->setEnabled(false);
            QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { link->setEnabled(true); });
        });
        link->move(20 + 120 * i, 320);
    }
    return btns;
}

ElCheckbox *MainWindow::testElCheckbox()
{
    auto checkbox = new ElCheckbox("Checkbox", this);
    checkbox->move(20, 380);
    // checkbox->setTristate(true);
    checkbox->setCheckState(Qt::Checked);
    checkbox->setBorder(true);
    QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
        checkbox->setEnabled(false);
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { checkbox->setEnabled(true); });
    });
    return checkbox;
}

void MainWindow::testElRadio()
{
    auto radio = new ElRadio("Radio", this);
    radio->move(20, 430);
    radio->setChecked(false);
    radio->setBorder(true);
    QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
        radio->setEnabled(false);
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { radio->setEnabled(true); });
    });
}

void MainWindow::testElSlider()
{
    auto slider = new ElSlider(this);
    slider->setValue(22);
    slider->move(20, 490);
    QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
        slider->setEnabled(false);
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { slider->setEnabled(true); });
    });
}

void MainWindow::testElSwitch()
{
    auto sw = new ElSwitch(this);
    // sw->setState(ElSwitch::Active);
    sw->setText(ElSwitch::Inactive, tr("关"));
    sw->setText(ElSwitch::Active, tr("开"));
    // sw->setForeground(ElSwitch::Inactive, Qt::green);
    // sw->setForeground(ElSwitch::Active, Qt::red);
    // sw->setBackground(ElSwitch::Inactive, Qt::black);
    // sw->setBackground(ElSwitch::Active, QColor::fromString("#fe2857"));
    sw->move(250, 490);
    QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() {
        sw->setEnabled(false);
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=]() { sw->setEnabled(true); });
    });
    connect(sw, &ElSwitch::toggled, this, [=](bool isActive) {
        qDebug() << isActive;
        sw->setCurrentText("我变了");
        qDebug() << sw->currentText();
    });
    connect(sw, &ElSwitch::stateChanged, this, [=](ElSwitch::State state) { qDebug() << state; });
}

void MainWindow::testElMessage(const QList<ElButton *> &btns)
{
    for (auto &btn : btns) {
        connect(btn, &ElAbstractButton::clicked, this, [=] {
            switch (btn->theme().type()) {
            case ElTheme::Success:
                ElMessage::success(this, tr("这是一条成功消息！"));
                break;
            case ElTheme::Warning:
                ElMessage::warning(this, tr("这是一条警告消息！"));
                break;
            case ElTheme::Danger:
                ElMessage::error(this, tr("这是一条错误消息！"));
                break;
            case ElTheme::Information:
                ElMessage::information(this,
                                       tr("这是一条停留5秒的可关闭的居中的提示消息！"),
                                       true,
                                       true,
                                       5000);
                break;
            default:
                ElMessage::information(this, tr("这是一条提示消息！"));
                break;
            }
        });
    }
}

void MainWindow::testElLoading()
{
    auto loading = new ElLoading(ui->groupBox, tr("加载中..."));
    auto font = loading->font();
    font.setPointSize(10);
    loading->setFont(font);
    // loading->setIcon(":/icons/warning.svg", 20);
    connect(loading, &ElLoading::clicked, this, [loading] { loading->hide(); });
    connect(loading, &ElLoading::disappeared, this, [loading, this] {
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [loading] { loading->show(); });
    });
}

void MainWindow::testElAlert()
{
    QString tips[] = {"成功", "警告", "消息", "错误"};
    for (int i = 0; i < 4; ++i) {
        auto alert = new ElAlert(this,
                                 tr("%1提示的文案").arg(tips[i]),
                                 tr("文字说明文字说明文字说明文字说明文字说明文字说明"));
        alert->setType(ElAlert::Type(i));
        // alert->setEffect(ElAlert::Dark);
        alert->setCenter(true);
        alert->setShowIcon(true);
        alert->setCloseText(tr("关闭"));
        // alert->setCloseBehavior(ElAlert::KeepOn);
        // connect(alert, &ElAlert::shutAfter, alert, &ElAlert::show);
        alert->move(20, 560 + (alert->height() + 30) * i);
    }
}

void MainWindow::testElNotification(ElCheckbox *chb)
{
    connect(chb, &ElCheckbox::clicked, this, [=] {
        ElNotification::success(this,
                                "成功提示",
                                "很长很长很长的文案",
                                3000,
                                ElNotification::TopRight,
                                false);
        ElNotification::warning(this,
                                "警告提示",
                                "很长很长很长的文案",
                                3000,
                                ElNotification::TopLeft,
                                false);
        ElNotification::info(this,
                             "消息提示",
                             "很长很长很长的文案",
                             3000,
                             ElNotification::BottomRight,
                             false);
        ElNotification::error(this,
                              "错误提示",
                              "很长很长很长的文案",
                              3000,
                              ElNotification::BottomLeft,
                              false);
        auto defaultNotify = new ElNotification(this,
                                                "默认无图标提示",
                                                "很长很长很长的文案",
                                                ElNotification::Default,
                                                0);
        connect(defaultNotify, &ElNotification::clicked, this, [=] {
            auto msg = new ElMessage(this, ElMessage::Success, "Yes!");
            msg->show();
            QTimer::singleShot(1000, Qt::CoarseTimer, this, [msg] {
                msg->setType(ElMessage::Error);
                msg->setMessage("No!");
            });
        });
        // defaultNotify->setClosable(false);
        defaultNotify->setPosition(ElNotification::BottomRight);
        // defaultNotify->setType(ElNotification::Success);
        defaultNotify->setIcon(":/icons/close.svg");
        // defaultNotify->setOffset(200);
        defaultNotify->show();
        // !!! don't delete any instance of ElNotification explicitly !!!
        // defaultNotify->deleteLater();// don't do that!
    });
}

void MainWindow::testElResult()
{
    QString tips[] = {tr("成功"), tr("警告"), tr("消息"), tr("失败")};
    for (int i = 0; i < 4; ++i) {
        auto rs = new ElResult(tr("%1提示").arg(tips[i]), tr("请按照提示进行操作"), this);
        rs->setIcon(ElResult::IconType(i));
        auto footer = new ElButton(tr("返回"), this);
        footer->setTheme(ElTheme::Primary);
        rs->setExtraFooter(footer);
        rs->move(465 + i * (rs->width() + 15), 560);
    }
    auto rs = new ElResult(tr("自定义图标"), QString(), this);
    rs->setIcon(":/icons/close.svg");
    auto footer = new ElButton(tr("返回"), this);
    footer->setTheme(ElTheme::Primary);
    rs->setExtraFooter(footer);
    rs->move(465 + 4 * (rs->width() + 15), 560);
}

void MainWindow::testElInput()
{
    auto input = new ElInput(this);
    input->editor()->setClearButtonEnabled(true);
    // input->editor()->setEchoMode(QLineEdit::Password);
    input->editor()->setPlaceholderText(tr("请输入密码"));
    input->setMaxLength(10);
    input->setShowWordLimit(true);
    // input->setShowPwdIcon(true);
    input->move(465, 720);
    // input->setAutoFocus(false);

    // prepend
    auto prepend_btn = new ElButton("http://", this);
    prepend_btn->setTheme(ElTheme::Primary);
    // auto lab = new QLabel("http://", this);
    // lab->resize(50, lab->height());
    // lab->setAlignment(Qt::AlignCenter);
    input->setPrepend(prepend_btn);

    // prefix
    auto prefix_icon = new ElIcon(":/icons/search.svg", this);
    prefix_icon->fillBackground(ThemeColor::textPrimaryColor);
    // auto txtBtn = new ElTextButton(this);
    // txtBtn->setIcon(icon);
    // txtBtn->setClipped(true);
    input->setPrefix(prefix_icon);

    // suffix
    auto suffix_icon = new ElIcon(":/icons/edit.svg", this);
    suffix_icon->fillBackground(ThemeColor::textPrimaryColor);
    input->setSuffix(suffix_icon);

    // append
    auto append_btn = new ElButton(tr("搜索"), this);
    append_btn->setTheme(ElTheme::Information);
    input->setAppend(append_btn);
    // auto input2 = new ElInput(this);
    // input2->move(640, 720);
    QTimer::singleShot(1500, Qt::CoarseTimer, this, [=] {
        input->setEnabled(false);
        // input2->setEnabled(false);
        QTimer::singleShot(1500, Qt::CoarseTimer, this, [=] {
            input->setEnabled(true);
            // input2->setEnabled(true);
        });
    });
}

void MainWindow::testElTag()
{
    QString tips[] = {tr("一"), tr("二"), tr("三"), tr("四"), tr("五")};
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 5; ++j) {
            auto tag = new ElTag(tr("标签%1").arg(tips[j]), this);
            tag->setClosable(true);
            tag->setEffect(ElTag::Effect(i));
            tag->setType(ElTag::Type(j));
            // tag->setHit(true);
            tag->move(750 + j * 100, 20 + i * 35);
            connect(tag, &ElTag::clicked, this, [=] {
                ElMessage::information(this, tr("标签被点击"));
            });
            connect(tag, &ElTag::closeClicked, this, [=] { tag->deleteLater(); });
        }
    }
    auto customTag = new ElTag(this);
    customTag->setContent("自定义颜色");
    customTag->setBackgroundColor(QColor(110, 53, 174));
    customTag->setForegroundColor(QColor(206, 155, 0));
    customTag->setBorderColor(Qt::blue);
    customTag->move(750, 125);
}

void MainWindow::testElDrawer()
{
    auto drawer = new ElDrawer(this);
    connect(drawer, &ElDrawer::closedBefore, drawer, &ElDrawer::fadeOut);
    connect(this, &MainWindow::resized, drawer, &ElDrawer::adjustfySize);
    drawer->setTitle("标题");
    drawer->setShowClose(false);
    // drawer->setModal(false);

    QWidget *content = new QWidget(this);
    auto c = new ElButton(tr("打开内层嵌套drawer"), content);
    connect(c, &ElButton::clicked, this, [=] {
        auto drawer2 = new ElDrawer(drawer);
        connect(drawer2, &ElDrawer::closedBefore, drawer2, &ElDrawer::fadeOut);
        int percent = 70;
        drawer2->setDrawerSize(percent);
        drawer2->setMaskClosable(false);
        drawer2->setTitle(
            tr("内层drawer，宽度（或高度）为第一个非ElDrawer的祖先窗口的%1%").arg(percent));
        drawer2->setCloseBehavior(ElDrawer::Delete); // drawer2 only show once then deleted.
        drawer2->show();
    });
    drawer->setContent(content);

    QString tips[] = {tr("从右往左"), tr("从左往右"), tr("从上往下"), tr("从下往上")};
    for (int i = 0; i < 4; ++i) {
        auto btn = new ElTextButton(tips[i], this);
        btn->setClipped(true);
        btn->move(750 + i * 60, 180);
        connect(btn, &ElTextButton::clicked, this, [=] {
            drawer->setDirection(ElDrawer::Direction(i));
            drawer->show();
        });
    }
}
